<template>
  <!-- 导入表 -->
  <el-dialog title="导入产品" :visible.sync="visible" width="800px" top="5vh">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item label="客户选择" prop="customerId">
        <el-select v-model="queryParams.customerId" placeholder="请选择客户" clearable size="small">
          <el-option
            v-for="dict in customerOptions"
            :key="dict.customerId"
            :label="dict.customerName"
            :value="dict.customerId"
          />
        </el-select>
<!--        <el-input-->
<!--          v-model="queryParams.customerId"-->
<!--          placeholder="请输入客户ID"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
      </el-form-item>
    </el-form>
    <el-row>
      <el-table @row-click="clickRow" ref="table" :data="dbTableList" @selection-change="handleSelectionChange" height="260px">
        <el-table-column type="selection" width="55" ></el-table-column>
        <el-table-column prop="productId" label="产品ID" v-if="false"></el-table-column>
        <el-table-column prop="productName" label="产品名称"></el-table-column>
        <el-table-column prop="productType" label="产品类别" :formatter="productTypeFormat"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleImportTable">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>m

<script>
  import {listProduct, importProduct} from "@/api/mtool/customerProduct";
  import {listCustomer} from "../../../api/mtool/customer";

  export default {
    data() {
      return {
        // 遮罩层
        visible: false,
        // 选中数组值
        tables: [],
        // 总条数
        total: 0,
        // 表数据
        dbTableList: [],
        // 产品类别字典
        productTypeOptions: [],
        // 客户字典
        customerOptions: [],
        // 产品字典
        productOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          tableName: undefined,
          tableComment: undefined
        }
      };
    },
    created() {
      this.getDicts("mtool_product_type").then(response => {
        this.productTypeOptions = response.data;
      });
      this.getCustomerDicts().then(response => {
        this.customerOptions = response.rows;
      });
      this.getProductDicts().then(response => {
        this.productOptions = response.rows;
      });
    },
    methods: {
      // 显示弹框
      show() {
        this.getList();
        this.visible = true;
      },
      clickRow(row) {
        this.$refs.table.toggleRowSelection(row);
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.tables = selection.map(item => item.productId);
      },
      // 查询表数据
      getList() {
        listProduct(this.queryParams).then(res => {
          if (res.code === 200) {
            this.dbTableList = res.rows;
            this.total = res.total;
          }
        });
      },
      // 产品类别字典翻译
      productTypeFormat(row, column) {
        return this.selectDictLabel(this.productTypeOptions, row.productType);
      },
      /** 查询客户产品关联列表 */
      getCustomerDicts() {
        return listCustomer();
      },
      /** 查询客户产品关联列表 */
      getProductDicts() {
        return listProduct()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 导入按钮操作 */
      handleImportTable() {
        console.log(this.tables)
        importProduct({idStr: this.tables.join(","),customerId:this.queryParams.customerId}).then(res => {
          this.msgSuccess(res.msg);
          if (res.code === 200) {
            this.visible = false;
            this.$emit("ok");
          }
        });
      }
    }
  };
</script>
